<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/9/7
 * Time: 14:43
 */
require('../../../common.inc.php');
define('SITE_AUTH', 'activity-line-matter');

use lib\core\AdminAction;
use Act\MaterialImg as Model;
use Common\ResourcePathManager;
use Common\Upload;

class matter extends AdminAction
{
    private $list_url = 'data/modules/activity/matter.php';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['Id >' => 0];

        $page = $_GET['page'] < 1 ? 1 : (int)$_GET['page'];
        $limit = 5;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit);
        $count = $data['count'];
        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }
        $smarty->output('activity/matter_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();
        $id = $_GET['id'];
        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $data = Model::getDataById($id);
            if (empty($data)) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $smarty->value('info', $data);
        }
        $smarty->output('activity/matter_edit.tpl');
    }

    public function doUpdata()
    {
        $id = (isset($_POST['id']) and $_POST['id'] !== '') ? intval(trim($_POST['id'])) : null;
        $imgUrl = $_FILES['img'];
        $url = trim($_POST['link']);
        $time = date('Y-m-d H:i:s');
        if ($id !== null) {
            $data = [
                'Url' => $url,
                'CreateTime' => $time,
            ];
            if (isset($imgUrl['name']) && $imgUrl['error'] == 0 && $imgUrl['size'] > 0) {
                $imgUrlAddre = Upload::saveFile($imgUrl['tmp_name'], ResourcePathManager::getMatterActivitySavePath());
                $data = array_merge($data, ['ImgUrl' => $imgUrlAddre]);
            }
            $rel = Model::update($id, $data);
        } else {
            if (isset($imgUrl['name']) && $imgUrl['error'] == 0 && $imgUrl['size'] > 0) {
                // 复制图片
                $imgUrlAddre = Upload::saveFile($imgUrl['tmp_name'], ResourcePathManager::getMatterActivitySavePath());
            } else {
                HDshowMsg('图片上传失败！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $data = [
                'Url' => $url,
                'CreateTime' => $time,
                'ImgUrl' => $imgUrlAddre,
            ];
            $rel = Model::add($data);
        }
        if($rel){
            HDshowMsg('保存成功', ['type' => 'success', 'url' => $this->list_url]);
        }else{
            HDshowMsg('操作失败', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

    public function doDelete(){
       $id =   $_POST['id'];
       $res = Model::delete($id);
       if($res){
            $this->output(['type'=>'yes']);
       }else{
           $this->output(['type'=>'yes']);
       }

    }
}

$app->run();